package com.example.limit.listener;

import com.alibaba.fastjson.JSON;
import com.example.limit.properties.RateLimiterConfig;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * 配置中心监听器（模拟版）
 */
@Component
public class ConfigRefreshListener {

    private final List<Consumer<RateLimiterConfig>> listeners = new ArrayList<>();

    public void onConfigChange(String json) {
        RateLimiterConfig newConfig = JSON.parseObject(json, RateLimiterConfig.class);
        listeners.forEach(l -> l.accept(newConfig));
    }

    public void addListener(Consumer<RateLimiterConfig> listener) {
        listeners.add(listener);
    }

}